<?php
$app = Blyn::app()->getActiveApp();

//定义添加成员的FORM的输入项
if (!isset($model))
    $model = new BDynamicFormModel ();

$memberNameField = 'media_name_buyerCategory_member';
$userIdField = 'media_userId_buyerCategory_member';
$roleIdField = 'media_roleId_buyerCategory_member';
$model->addField($memberNameField);
$model->addField($userIdField);
$model->addField($roleIdField);

//获取已经添加的成员列表
$categoryMemberView = array();
if (isset($categoryId) && $categoryId > 0) {
    $buyerCategoryTable = new BPSheet('buyerCategory');
    $category = $buyerCategoryTable->getListById($categoryId);
    $buyerCategoryMemberTable = new BPSheet('buyerCategoryMember', $category);
    $categoryMemberView = $buyerCategoryMemberTable->getView();
}
?>

<link rel="stylesheet" type="text/css"
      href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-datetimepicker.min.css" />
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-datetimepicker.min.js"/>

<script type="text/javascript">
    $(document).ready(function() {
        $("#selectAll").click(function() {
            if ($(this).attr("checked") == "checked") {
                $(":checkbox").attr("checked", true);
            } else {
                $(":checkbox").attr("checked", false);
            }
        });

    });
</script> 

<script type="text/javascript">
    $(function() {
        $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
        $('.tree li.parent_li > span').on('click', function(e) {
            var children = $(this).parent('li.parent_li').find(' > ul > li');
            if (children.is(":visible")) {
                children.hide('fast');
                $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
            } else {
                children.show('fast');
                $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
            }
            e.stopPropagation();
        });
    });</script>
<script type="text/javascript">
    $(function() {
        $("#Event_Shop_CX").click(function() {
            document.getElementById("Event_Shop_CX_View").style.display = '';
            document.getElementById("Event_Clothes_CX_View").style.display = 'none';
        });
        $("#Event_Clothes_CX").click(function() {
            document.getElementById("Event_Shop_CX_View").style.display = 'none';
            document.getElementById("Event_Clothes_CX_View").style.display = '';
        });
    });</script>
<?php ?>
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "addBuyerCategoryMemberForm",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
?>
<legend>
    <h4>
        <?php echo $control->getAjaxLink(array('name' => '活动列表', 'id' => 'showEventList')); ?>\
        <?php echo $control->getAjaxLink(array('name' => '买家分类列表', 'id' => 'showBuyerCategoryList'), array('eventId' => $eventId)); ?>\
        <?php echo '设置分类成员'; ?>
    </h4>
</legend>
<!--首先添加FORM来添加新MEMBER -->
<div class="well-small row-fluid">
    <div class="input-prepend span3">
        <span class="add-on">定义名称</span>
        <?php
        echo CHtml::activeTextField($model, $memberNameField, array('placeholder' => 'name' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">角色名称</span>
        <?php
        $roleList = $app->getCustomerRoles();
        echo CHtml::activeDropDownList($model, $roleIdField, $roleList, array('required' => true, 'placeholder' => 'roleId' . '...', 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">用户ID</span>
        <?php
        echo CHtml::activeTextField($model, $userIdField, array('placeholder' => 'userId' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-append span2">
        <?php
        echo $control->getAjaxLink(array('name' => '添加成员', 'id' => 'manageBuyerCategoryMember'), array(
            'htmlOptions' => array('class' => 'btn'),
            'categoryId' => $categoryId,
            'eventId' => $eventId,
            'categoryMemberId' => 'newMember'
        ));
        ?>
    </div>
</div>
<!-- BELOW 显示列表 -->
<fieldset>
    <table class="table table-hover">
        <thead>
            <tr>
                <th>
                    <input type="checkbox"  id="selectAll"> 
                </th>
                <th><?php echo '定义名称'; ?></th>
                <th><?php echo '用户ID'; ?></th>
                <th><?php echo '角色名称'; ?></th>
                <th><?php echo '操作'; ?></th>
            </tr> 
        </thead>
        <tbody>
            <?php
            foreach ($categoryMemberView as $member) {
                $memberId = $member->getId();
                ?>
                <tr>
                    <td><label class="checkbox">
                            <input type="checkbox" name="checkbox[]" value="<?php echo $member->getId(); ?>"> 
                        </label></td>
                    <td> <?php echo $member->getItem('name')->getValue(); ?> </td>
                    <td> <?php
                        $userIdItem = $member->getItem('userId');
                        if ($userIdItem != NULL)
                            echo $userIdItem->getValue();
                        else {
                            echo '---';
                        }
                        ?> 
                    </td>
                    <td> <?php
                        $roleIdItem = $member->getItem('roleId');
                        if ($roleIdItem != NULL) {
                            $roleId = $roleIdItem->getValue();
                            $list = $app->getCustomerRoles();
                            echo $list[$roleId];
                        }  else {
                            echo '---';
                        }
                        ?> 
                    </td> 
                    <td>
                        <div class="row-fluid">                         
                            <?php
                            echo $control->getAjaxLink(array('name' => '删除', 'id' => 'deleteBuyerCategoryMember'), array(
                                'buyerCategoryMemberId' => $memberId,
                                'categoryId' => $category->getId(),
                                'eventId' => $eventId
                                    //'ajaxContainerId' => 'myModal',
                                    //'htmlOptions' => array(
                                    //'enctype' => 'multipart/form-data'),
                                    //'ajaxOptions' => array(
                                    //'complete' => 'function(){$("#ajax-loaderdiv").hide();$("#myModal").modal("show");}')
                            ));
                            ?>                               

                        </div>                      
                    </td>
                </tr>
            <?php } ?>
        </tbody>
    </table>
    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    </div>

    <div><input type="hidden" name="curPageNo" value="1"></div>

    <div class="pagination">  
        <ul>  
            <li class="active">
                <a href="javascript:void()">前一页</a>    </li>  
            <li class="active">
                <a href="javascript:void()">1</a>    </li>  
            <li class="active">
                <a href="javascript:void()">后一页</a>    </li>  
        </ul>  
    </div>
    <?php
    $this->endWidget();
//echo CHtml::endForm();
    ?>


